package workRequestServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swim2.sessionbeans.*;

public class FeedbackInsertionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public FeedbackInsertionServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		try{
			Context ctx = getInitialContext();			
			UserSystemRemote userSystemRemote = (UserSystemRemote) ctx.lookup("userJNDI");
			
			int outcome;
			if(Integer.parseInt(request.getParameter("recipientId"))!=0 && Integer.parseInt(request.getParameter("rate"))!=0 && !request.getParameter("title").equals("")){
				
				String title = request.getParameter("title");
				int recipientId = Integer.parseInt(request.getParameter("recipientId"));
				int rate = Integer.parseInt(request.getParameter("rate"));
				int abilityId = Integer.parseInt(request.getParameter("abilityId"));
				int authorId = Integer.parseInt(request.getParameter("authorId"));
				int wrId = Integer.parseInt(request.getParameter("wrId"));
				outcome = userSystemRemote.insertFeedback(title, authorId, recipientId, abilityId, rate, wrId);
				String userMail = userSystemRemote.getMail(authorId);
				String userPassword = userSystemRemote.getPassword(authorId);
				request.setAttribute("userId", authorId);
				request.setAttribute("wrId", wrId);
				request.setAttribute("outcome", outcome);
				request.setAttribute("userMail", userMail);
				request.setAttribute("userPassword", userPassword);
				RequestDispatcher dispatcher =
						request.getRequestDispatcher("/WorkRequestJsp/FeedbackInsertion.jsp");
				dispatcher.forward(request, response);
			}
			else{
				int authorId = Integer.parseInt(request.getParameter("authorId"));
				int wrId = Integer.parseInt(request.getParameter("wrId"));
				outcome = -1;
				String userMail = userSystemRemote.getMail(authorId);
				String userPassword = userSystemRemote.getPassword(authorId);	
				request.setAttribute("userId", authorId);
				request.setAttribute("wrId", wrId);
				request.setAttribute("outcome", outcome);
				request.setAttribute("userMail", userMail);
				request.setAttribute("userPassword", userPassword);
				RequestDispatcher dispatcher =
						request.getRequestDispatcher("/WorkRequestJsp/FeedbackInsertion.jsp");
				dispatcher.forward(request, response);	
			}
			
				
		}
		catch (Exception e) {
			e.printStackTrace();
			out.print("Feedback insertion exception");
		}
	}

	static public Context getInitialContext() throws Exception{
		Hashtable<String,String> env = new Hashtable<String,String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL, "localhost:1099");
		return new InitialContext(env);
	}
}
